package com.ljt.yupictureddd.application.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ljt.yupictureddd.domain.user.service.UserDomainService;
import com.ljt.yupictureddd.infrastracture.common.BaseResponse;
import com.ljt.yupictureddd.infrastracture.common.DeleteRequest;
import com.ljt.yupictureddd.infrastracture.common.ResultUtils;
import com.ljt.yupictureddd.infrastracture.exception.BusinessException;
import com.ljt.yupictureddd.infrastracture.exception.ErrorCode;
import com.ljt.yupictureddd.infrastracture.exception.ThrowUtils;
import com.ljt.yupictureddd.interfaces.dto.user.UserAddRequest;
import com.ljt.yupictureddd.interfaces.dto.user.UserQueryRequest;
import com.ljt.yupictureddd.interfaces.dto.user.UserRegisterRequest;
import com.ljt.yupictureddd.domain.user.entity.User;
import com.ljt.yupictureddd.interfaces.dto.user.UserUpdateRequest;
import com.ljt.yupictureddd.interfaces.vo.user.LoginUserVO;
import com.ljt.yupictureddd.interfaces.vo.user.UserVO;
import com.ljt.yupictureddd.application.service.UserApplicationService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Collection;
import java.util.List;

/**
 * @author 六件套
 * @description 针对表【user(用户)】的数据库操作Service实现
 * @createDate 2025-03-05 19:37:54
 */
@Service
@Slf4j
@AllArgsConstructor
public class UserApplicationServiceImpl implements UserApplicationService {


    private final UserDomainService userDomainService;

    /**
     * 用户注册
     *
     * @param request
     * @return
     */
    @Override
    public long userRegister(UserRegisterRequest request) {
        User.validRegister(request);
        return userDomainService.userRegister(request);
    }

    @Override
    public LoginUserVO userLogin(String userAccount, String userPassword, HttpServletRequest request) {
        User.validLogin(userAccount,userPassword,request);
        return userDomainService.userLogin(userAccount,userPassword,request);
    }

    /**
     * 获取当前User
     *
     * @param request
     * @return
     */
    @Override
    public User getLoginUser(HttpServletRequest request) {
        return userDomainService.getLoginUser(request);
    }

    /**
     * 用户退出系统
     *
     * @param request
     * @return
     */
    @Override
    public boolean userLogout(HttpServletRequest request) {
        return userDomainService.userLogout(request);
    }

    @Override
    public QueryWrapper<User> getQueryWrapper(UserQueryRequest request) {
        return userDomainService.getQueryWrapper(request);
    }

    /**
     * 获得脱敏的登陆用户VO
     *
     * @param user 数据库查出来的user
     * @return 脱敏的用户VO
     */
    @Override
    public LoginUserVO getLoginUserVO(User user) {
        return userDomainService.getLoginUserVO(user);
    }

    /**
     * 转换用户脱敏后的用户VO
     * @param user
     * @return
     */
    public UserVO getUserVO(User user){
        return userDomainService.getUserVO(user);
    }

    /**
     * 转换用户脱敏后的用户VO
     * @param user
     * @return
     */
    public List<UserVO> getUserVOList(List<User> user){
        return userDomainService.getUserVOList(user);
    }

    /**
     * 新增用户
     * @param userAddRequest
     * @return
     */
    @Override
    public BaseResponse addUser(UserAddRequest userAddRequest) {
        ThrowUtils.throwIf(userAddRequest==null, ErrorCode.PARAMS_ERROR);
        User user = new User();
        BeanUtil.copyProperties(userAddRequest,user);
//        默认密码
        final String DEFAULT_PASSWORD = "12345678";
        String encryptedPassword = userDomainService.getEncryptedPassword(DEFAULT_PASSWORD);
        user.setUserPassword(encryptedPassword);
//        插入数据库
        boolean save = userDomainService.save(user);
        ThrowUtils.throwIf(!save,ErrorCode.SYSTEM_ERROR);
        return ResultUtils.success(save);
    }

    /**
     * 根据 id 获取用户（仅管理员）
     */
    @Override
    public BaseResponse<User> getUserById(long id) {
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userDomainService.getById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(user);
    }

    /**
     * 删除用户
     */
    @Override
    public BaseResponse<Boolean> deleteUser(DeleteRequest deleteRequest) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean b = userDomainService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户
     */
    @Override
    public BaseResponse<Boolean> updateUser(UserUpdateRequest userUpdateRequest) {
        if (userUpdateRequest == null || userUpdateRequest.getId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = new User();
        BeanUtils.copyProperties(userUpdateRequest, user);
        boolean result = userDomainService.updateById(user);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        return ResultUtils.success(true);
    }

    /**
     * 分页
     */
    @Override
    public BaseResponse listUserVOByPage(UserQueryRequest userQueryRequest) {
        ThrowUtils.throwIf(userQueryRequest==null,ErrorCode.PARAMS_ERROR);
        int current = userQueryRequest.getCurrent();
        int pageSize = userQueryRequest.getPageSize();

        Page<User> page = userDomainService.page(new Page<>(current, pageSize), userDomainService.getQueryWrapper(userQueryRequest));

//        准备一个脱敏的列表页面
        List<UserVO> userVOList = userDomainService.getUserVOList(page.getRecords());
        Page<UserVO> userVOPage = new Page<>(current, pageSize,page.getTotal());
        userVOPage.setRecords(userVOList);

        return ResultUtils.success(userVOPage);
    }

    @Override
    public User getById(Long userId) {
        return userDomainService.getById(userId);
    }

    @Override
    public List<User> listByIds(Collection<Long> userIdSet) {
        return userDomainService.listByIds(userIdSet);
    }

    /**
     * 密码加密
     *
     * @param userPassword 注册登陆的提交的密码
     * @return 加密后的密码
     */
    public String getEncryptedPassword(String userPassword) {
        return userDomainService.getEncryptedPassword(userPassword);
    }
}




